﻿using Castle.Core.Logging;
using com.ytocargo.Application.BackgroundJobs;
using com.ytocargo.Core.AirPortCharts;
using com.ytocargo.Core.Charts;
using com.ytocargo.Core.GeneralDocs;
using System;
using System.Configuration;
using System.Diagnostics;
using Yto.Common;
using Yto.Common.Dependency;

namespace com.ytocargo.Application.ChartsInPeriods
{
    public class InPeriodWorker : BackgroundJob<InPeriodWorkerArgs>, ITransient
    {
        private IChartStore chartStore;
        private ILogger _logger;
        private IGeneralDocStore _generalDocStore;
        private IAirPortChartStore _airPortChartStore;
        private ICtnManager _ctnManager;

        public InPeriodWorker(ICtnManager ctnManager, 
            IChartStore chart,
            IGeneralDocStore generalDocStore,
            IAirPortChartStore airPortChartStore)
        {
            _ctnManager = ctnManager;
            chartStore = chart;
            _logger = ctnManager.IsRegisted<ILoggerFactory>() ?
                ctnManager.Resolver<ILoggerFactory>().Create(typeof(InPeriodWorker))
                : NullLogger.Instance;
            _generalDocStore = generalDocStore;
            _airPortChartStore = airPortChartStore;
        }

        public override void Execute(InPeriodWorkerArgs Args)
        {
            try
            {
                string strFilePath = ConfigurationManager.AppSettings["CsvPathCurrent"];
                string csvPathToUpdate = ConfigurationManager.AppSettings["CsvPathToUpdate"];
                string strChartName = ConfigurationManager.AppSettings["ChartsFile"];
                string format = ConfigurationManager.AppSettings["DateTimeFormat"];
                string generalDocFileName = ConfigurationManager.AppSettings["GeneralDocFile"];

                //var files = FileHelper.GetAllFile(csvPathToUpdate);
                //foreach(var item in files)
                //{
                //    FileHelper.Move2Path(item.FullName, strFilePath, false);
                //}
                FileHelper.FolderMove(csvPathToUpdate, strFilePath);

                if (FileHelper.FileExist(strFilePath + strChartName))
                {

                    var Charts = CsvFileHelper.GetList<Chart>(strFilePath + strChartName, format, typeof(ChartMap));
                    chartStore.DeleteAllAsync();
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    chartStore.BatchInsertAsync(Charts);
                    sw.Stop();
                    _logger.Info($"批量添加Chart用时： { sw.ElapsedMilliseconds }");

                }

                if (FileHelper.FileExist(strFilePath + generalDocFileName))
                {
                    var GeneralDocs = CsvFileHelper.GetList<GeneralDoc>(strFilePath + generalDocFileName, format, typeof(GeneralDocMap));
                    _generalDocStore.DeleteAllAsync();
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    _generalDocStore.BatchInsertAsync(GeneralDocs);
                    sw.Stop();
                    _logger.Info($"批量添加GeneralDoc用时： { sw.ElapsedMilliseconds }");

                }


                _airPortChartStore.DeleteAllAsync();
                _logger.Info($"删除airPortChart(T3093)数据： { DateTime.Now }");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
            }

        }

    }
}
